const users = require('../mongodb/users')
const mongodb = require('../mongodb/index')
const fs = require('fs')
const path = require('path')

module.exports = function (){
    return (req,res,next)=>{
        const obj = req.body
        // obj.tel -= 0
        // obj.hobby = obj.hobby.split(/,|\uff0c/)

        // const oldName = path.join(__dirname,'..',req.files[0].path)
        // const newName = oldName + '-' + req.files[0].originalname
        // fs.renameSync(oldName,newName)
        // const serverUrl = 'http://localhost:65535' + newName.split('\\user_upload')[1]
        // console.log(newName.split('\\user_upload'))
        // obj.avatar = serverUrl

        // console.log(obj)
        if(obj.surepw){
            mongodb.set(users,{username:obj.username},{password:obj.surepw},()=>{
                res.send('密码修改成功')
            })
        }
        
        if(obj.newname){
            mongodb.set(users,{username:obj.username},{username:obj.newname},()=>{
                req.session.username = obj.newname;
                res.send('昵称修改成功')
            })
        }
    }
}